package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 查询今日运营数据
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);
        map.put("status", Orders.COMPLETED);


        // 营业额 turnover 汇总当日已完成订单的金额
        //select sum(amount) from orders where status = 5 and order_time >= #{begin} and order_time <=#{end}
        Double turnover = orderMapper.sumByMap(map);
        turnover = turnover == null ? 0.0 : turnover;


        // 有效订单 validOrderCount 汇总当日已完成状态的订单总条数
        //select count(id) from orders where status = 5 and order_time >= #{begin} and order_time <=#{end}
        Integer validOrderCount = orderMapper.countOrdersByMap(map);


        // 总订单数 totalOrderCount
        //select count(id) from orders where order_time >= #{begin} and order_time <=#{end}
        map.remove("status");
        Integer totalOrderCount = orderMapper.countOrdersByMap(map);


        // 订单完成率 oederCompletionRate 有效订单数/总订单数
        Double oederCompletionRate = 0.0;
        if (totalOrderCount != 0){
            oederCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
        }


        // 平均客单价 unitPrice 营业额/有效订单数
        Double unitPrice = 0.0;
        if (validOrderCount != 0){
            unitPrice = turnover / validOrderCount;
        }


        // 新增用户数 nweUsers 汇总当日新增用户数量
        //select count(id) from user where create_time >= #{begin} and create_time <=#{end}
        Integer newUsers = userMapper.countUserByMap(map);

        //封装BusinessDataVO
        BusinessDataVO businessDataVO = BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(oederCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
        return businessDataVO;
    }


    /**
     * 查询订单管理数据
     * @return
     */
    @Override
    public OrderOverViewVO getOrderOverView() {
        //获得当天的开始时间
        LocalDateTime begin = LocalDateTime.now().with(LocalTime.MIN);
        //获得当天的结束时间
        LocalDateTime end = LocalDateTime.now().with(LocalTime.MAX);


        //select count(id) from orders where order_time >= #{begin} and oeder_time <= #{end} and status = #{status}
        Map map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);

        //全部订单 allOders
        Integer allOrders = orderMapper.countOrdersByMap(map);
        //已取消数量 cancelledOrders
        map.put("status",Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.countOrdersByMap(map);
        //已完成数量 comopletedOrders
        map.put("status",Orders.COMPLETED);
        Integer completedOrders = orderMapper.countOrdersByMap(map);
        //待派送数量 deliveredOrders
        map.put("status",Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.countOrdersByMap(map);
        //待接单数量 waittingOrders
        map.put("status", Orders.TO_BE_CONFIRMED);
        Integer waittingOrders = orderMapper.countOrdersByMap(map);

        //封装OrderOverViewVO
        OrderOverViewVO orderOverViewVO = OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waittingOrders)
                .build();
        return orderOverViewVO;
    }


    /**
     * 查询菜品总览
     * @return
     */
    @Override
    public DishOverViewVO getDishOverView() {
        Map map = new HashMap<>();

        //select count(id) from dish where status = #{status}
        // 已启售数量 sold
        map.put("status", StatusConstant.ENABLE);
        Integer sold = dishMapper.countByMap(map);

        // 已停售数量 discontinued
        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = dishMapper.countByMap(map);

        //封装DishOverViewVO
        DishOverViewVO dishOverViewVO = DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
        return dishOverViewVO;
    }


    /**
     * 查询套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO getSetmealOverView() {
        Map map = new HashMap<>();

        //select count(id) from setmeal where status = #{status}
        // 已启售数量 sold
        map.put("status", StatusConstant.ENABLE);
        Integer sold = setmealMapper.countByMap(map);

        // 已停售数量 discontinued
        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = setmealMapper.countByMap(map);

        //封装SetmealOverViewVO
        SetmealOverViewVO setmealOverViewVO = SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();

        return setmealOverViewVO;
    }
}
